package by.bsu.wp.ntfsp;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class LatexConverter extends HttpServlet {

	public LatexConverter() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		try {
			List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
			for (FileItem item : items) {
				if (!item.isFormField()) {
					
//					InputStream filecontent = item.getInputStream();
//					BufferedReader reader = new BufferedReader(new InputStreamReader(filecontent));
//
//					NFRSProblemParser parser = new NFRSProblemParser();
//					NotFullRankSystemProblem problem = parser.parse(reader);
//
//					//NFRSProblemLatexConverter conv = new NFRSProblemLatexConverter();
//					String latexResult = conv.convertToLatex(problem);
//					
//					//LatexAdapter latexAdapter = new LatexAdapter();
//					//String imageUrl = latexAdapter.requestImage(latexResult);
//					System.out.println("sss");
//					Gson gson = new Gson();
//					System.out.println(gson.toJson(problem));
//					
//					response.setContentType("text/plain;charset=UTF-8");
//					response.setCharacterEncoding("UTF-8");
//					response.getWriter().write(latexResult == null ? "" : latexResult);
//					response.getWriter().close();
				}
			}
		} catch (FileUploadException exception) {
			throw new ServletException("Cannot parse multipart request.", exception);
		}
	}

}
